package com.copa.module.order.controller;

import com.copa.domain.ResponseDto;
import com.copa.module.order.domain.dto.OrderDetailDto;
import com.copa.module.order.domain.dto.OrderListDto;
import com.copa.module.order.domain.vo.OrderAddVo;
import com.copa.module.order.domain.vo.OrderRefundVo;
import com.copa.module.order.service.OrderService;
import com.copa.utils.WechatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-25 11:58
 * @function
 */
@RestController
@Api(tags = {"订单接口"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order/post")
    @ApiOperation(value = "创建订单")
    public ResponseDto<String> post(@RequestBody OrderAddVo orderAddVo,
                                    @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return orderService.post(orderAddVo, openId);
    }

    @GetMapping("/order/list")
    @ApiOperation(value = "查看订单列表")
    public ResponseDto<List<OrderListDto>> list(@ApiParam("订单状态") @RequestParam("orderStatus") Long orderStatus,
                                               @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return orderService.list(orderStatus, openId);
    }

    @GetMapping("/order/cancel")
    @ApiOperation(value = "用户取消订单")
    public ResponseDto<String> cancel(@ApiParam("订单状态") @RequestParam("orderId") Long orderId,
                                    @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return orderService.cancel(orderId, openId);
    }

    @GetMapping("/order/detail")
    @ApiOperation(value = "订单详情")
    public ResponseDto<OrderDetailDto> detail(@ApiParam("订单id") @RequestParam("orderId") Long orderId,
                                    @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return orderService.detail(orderId, openId);
    }

    @PostMapping("/order/applyRefund")
    @ApiOperation(value = "单个订单商品申请退款")
    public ResponseDto<String> applyRefund(@RequestBody OrderRefundVo orderRefundVo,
                                      @AuthenticationPrincipal Principal principal) {
        String openId = WechatUtils.getOpenId(principal.getName());
        return orderService.applyRefund(orderRefundVo, openId);
    }

}
